<template>
  <div class="login-container">
    <div class="login-box">
      <div class="autor-login">
        <img src="../assets/logo.png" alt>
      </div>
      <el-form ref="formRef" :model="userList" label-width="0px" :rules="formRules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="userList.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="userList.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      userList: {
        username: "admin",
        password: "123456"
      },
      formRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    async submitForm() {
      const { data: res } = await this.$http.post("login", this.userList);
      if (res.meta.status !== 200) return this.$message.error("登录失败");
      this.$message.success("登录成功");
      this.$router.push("/home");
      window.sessionStorage.setItem("token", res.data.token);
    },
    resetForm() {
      this.$refs.formRef.resetFields();
    }
  }
};
</script>
<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 430px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  > div {
    width: 160px;
    height: 160px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 1px solid #ccc;
    background-color: #fff;
    box-shadow: 0 0 7px rgba(0, 0, 0, 0.15);
    img {
      width: 100%;
      border-radius: 50%;
    }
  }
}
.el-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
